
// 防抖
export const shakeProof = (fn, wait) => {
  var timer = null
  return function() {
    if (timer !== null) {
      clearTimeout(timer)
    }
    timer = setTimeout(fn, wait)
  }
}
// 节流
export const throttle = (fn, wait) => {
  var timer = null
  return function() {
    var context = this
    var args = arguments
    if (!timer) {
      timer = setTimeout(function() {
        fn.apply(context, args)
        timer = null
      }, wait)
    }
  }
}

export const formatTime = (time) => {
  if (typeof time !== 'number' || time < 0) {
    return time
  }

  var hour = parseInt(time / 3600)
  time = time % 3600
  var minute = parseInt(time / 60)
  time = time % 60
  var second = time

  return ([hour, minute, second]).map(function(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  }).join(':')
}

export const formatLocation = (longitude, latitude) => {
  if (typeof longitude === 'string' && typeof latitude === 'string') {
    longitude = parseFloat(longitude)
    latitude = parseFloat(latitude)
  }

  longitude = longitude.toFixed(2)
  latitude = latitude.toFixed(2)

  return {
    longitude: longitude.toString().split('.'),
    latitude: latitude.toString().split('.')
  }
}
// 倒计时
export const timeDown = (timeNum, fn) => {
  var s = timeNum
  var timer = setInterval(function() {
    s--
    if (s === 0) {
      clearInterval(timer)
      s = timeNum
    }
    return fn(s)
  }, 1000)
}
// 计算倒计时 按照天，时，分，秒
export const getCountDown = (endTime) => {
  // endTime = 1614155555
  const nowDate = new Date()
  const nowTime = parseInt(nowDate.getTime() / 1000) // 当前时间戳改为10位,因为后台返回的时间戳是10位
  const t = endTime - nowTime
  if (t <= 0) {
    return false
  }
  let day = Math.floor(t / 60 / 60 / 24)
  let hour = Math.floor(t / 60 / 60 % 24)
  let minute = Math.floor(t / 60 % 60)
  let second = Math.floor(t % 60)
  // if (day < 10) {
  //   day = '0' + day TODO: padStart是字符串方法
  // }
  day = String(day).padStart(2, '0')
  hour = String(hour).padStart(2, '0')
  minute = String(minute).padStart(2, '0')
  second = String(second).padStart(2, '0')
  // if (hour < 10) {
  //   // hour = '0' + hour
  // }
  // if (minute < 10) {
  //   // minute = '0' + minute
  // }
  // if (second < 10) {
  //   // second = '0' + second
  // }
  return { day, hour, minute, second }
}
// 获取对应的cookie值
export const getCookieName = (name) => {
  const cookie = document.cookie
  // console.log(cookie)
  // const cookie = '__ISHIDDENACTIVITYNAV__=1; __ISNATIVE__=1; platform=iOS'
  var reg = new RegExp('(^|; )' + name + '=([^;]*)(;|$)') // 获取platform的值
  // console.log('进入reg', reg)
  var arr2 = cookie.match(reg)
  // console.log(arr2)
  // console.log(arr2[2])
  var val = arr2 ? arr2[2] : ''
  return val
}
// 时间

export const remainTime = (timeSpacing) => {
  let day = 0
  let hour = 0
  let minute = 0
  let second = 0
  // let second = 0
  day = Math.floor(timeSpacing / (24 * 60 * 60 * 1000))
  hour = Math.floor(timeSpacing % (24 * 60 * 60 * 1000) / (60 * 60 * 1000))
  minute = Math.floor((timeSpacing - day * 24 * 60 * 60 * 1000) % (60 * 60 * 1000) / (60 * 1000))
  second = Math.floor((timeSpacing - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000) % (60 * 1000) / 1000)
  return ` ${day > 0 ? day + '天' : ''}  ${hour}  小时  ${minute}  分钟  ${second}  秒`
}
 

// 价格截取整数
export const cuntInit = (price) => {
  const index = String(price).indexOf('.')
  const init = String(price).substring(0, index)
  return init
}
// 价格截取小数
export const countDeci = (price) => {
  const index = String(price).indexOf('.')
  const deci = String(price).substring(index)
  return deci
}

// 解析分享scene参数
export const getKey = function(str, key) {
  const reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)')
  const matchStr = str.match(reg)
  return matchStr ? decodeURI(matchStr[2]) : ''
}
